<template>
	<view >
		<!-- :show="show" -->
		<u-overlay :show="show" >
			<view class="warp">
				<view class="rect" @tap.stop >
					<view style="padding: 20rpx;" >
						<view style="margin-bottom: 32rpx;">
							<u--text text='新版本' size='30rpx'  align='center'></u--text>
						</view>
						<u-row v-for="item in text">
							<text class="etxt" >{{item}}</text>
						</u-row>
						<view v-if="!isUpdate" style="position: absolute;width: 360rpx; bottom: 20rpx;">
							<u-row justify="around">
								 <u-col span="4" >
									 <u-button size="small" type="primary" text="更新" @click="downLoad"></u-button>
								 </u-col>
								 <u-col span="4"   >
									 <u-button size="small" type="primary" @click="show = false" :plain="true" text="取消"></u-button>
								 </u-col>
									
							 
							</u-row>
						</view>
					</view> 
					 <u-row v-if=" isUpdate"  style="position: absolute;width: 100%;bottom: 10rpx;" >
						 <u-col span="10" offset="1" style=" height: 50rpx;position: relative;">
							 <text text="下载中..." align='center' style="position: absolute;width: 100%;z-index: 200;height: 45rpx; font-size: 25rpx;">下载中...</text>
							 <u-line-progress :showText='false'   height='45rpx'  :percentage="plan" activeColor="#15cdff"></u-line-progress>
						 </u-col>
					 </u-row>
				 
				</view> 
			</view>
		</u-overlay>
		
	</view>
</template>

<script>
	import  AppInfo from "../../appInfo.js"
	import NetTool from '@/common/js/axion.js'
	let that
	export default {
		name:"update-app",
		 
		computed:{
			text(){ 
				let a = this.remark.split(',')
				return a
			}
		},
		data() {
			return {
				show:false,
				remark:'',
				plan:0,
				isUpdate:false,
				newAppInfo:{}
			};
		},
		mounted() {
			that = this
			console.log('组件驾照')
			let appInfo = AppInfo
			let mNetTool = new NetTool()
			let getVer = function(str){
				let newStr = ''
				for(let item of str){
					if(typeof (item - 0) == 'number' && !isNaN((item - 0))){
						newStr += item
					}  
				}
							 
				return  newStr 
			}
			let nversion = Number(getVer(appInfo.version))
			let version 
			let appName = appInfo.appName
			 console.log('当前版本',nversion)
			mNetTool.get('capi/V2/sysSetting/coreAppUpdate/nf/list',{
				 name:appName
			 },res=>{
				  
				if(res.rows.length == 0){
					// uni.$u.toast('暂无更新')
				 
				}else{
					version = res.rows[0].version_code
					if(nversion ==version){
						uni.$u.toast('当前已是最新版')
					}else if(nversion < version){
						uni.hideLoading()
						if(!res.rows[0].remark){
							 remark = ' '
						}
						that.remark = res.rows[0].remark
						that.show = true
						that.newAppInfo = res.rows[0]
						// uni.showModal({
						// 	title: '提示',
						// 	content: '有新的版本是否更新。'+res.rows[0].remark,
						// 	showCancel: true,
						// 	cancelText: '取消',
						// 	confirmText: '更新',
						// 	success: r => {
						// 		if(r.confirm){
									// UpdateOfWgt(res.rows[0])
						// 		}
						// 	},
						// 	fail: () => {},
						// 	complete: () => {}
						// });
					}
				}
			}) 
		},
		methods:{
			downLoad(data){
				that.isUpdate = true
				data = that.newAppInfo
				// #ifdef APP-PLUS
				 const downloadTask = uni.downloadFile({
					url: data.url,  
					success: (downloadResult) => { 
						uni.hideLoading()
						if (downloadResult.statusCode === 200) {  
							console.log("增量更新包下载成功，即将安装: " + JSON.stringify(downloadResult, null, 4));
							plus.runtime.install(downloadResult.tempFilePath, {  
								force: false  
							}, function() {
								console.log("增量更新成功,将重启app");
								plus.runtime.restart();  
							}, function(e) {
								console.error('增量更新安装失败：' + JSON.stringify(e));  
							});  
						}
						// uni.hideLoading()
					},
					
				});
				let pass = 0
				// uni.showLoading({
				// 	title:'下载进度'+ pass.toFixed(0) + '%'
				// })
				downloadTask.onProgressUpdate((res) => {
					pass = res.totalBytesWritten/res.totalBytesExpectedToWrite
					// console.log('下载进度' + progress);
					let p = pass.toFixed(2) 
					that.plan = p * 100 
					 console.log('已下载' + that.plan);
					// console.log('已经下载的数据长度' + res.totalBytesWritten);
					// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
				});
				// #endif
			}
		}
	}
</script>

<style lang="scss">
.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}

	.rect {
		box-shadow: rgba(0, 0, 0, 0.25) 0px 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;
		border-radius: 15rpx;
		width: 400rpx;
		height: 520rpx;
		background-color: #fff;
		position: relative;
		
		
	}
	.etxt{ 
		word-wrap:break-word;
	}
</style>
